package com.xuexi.controller;

import com.easygo.utils.MessageResults;
import com.easygo.utils.PageUtils;
import com.xuexi.api.ContentCategoryClient;
import com.xuexi.api.ContentClient;
import com.xuexi.easygo.pojo.Content;
import com.xuexi.easygo.pojo.ContentCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class ContentController {
    @Resource
    ContentClient contentClient;

    @Autowired
    ContentCategoryClient contentCategoryClient;

    @RequestMapping("content_page")
    public String getContent_Page(@RequestParam(value = "pageIndex",defaultValue = "1",required = false) Integer pageIndex,
                                  @RequestParam(value = "pageSize",defaultValue = "5",required = false)Integer pageSize,
                                  Model model){
        PageUtils<Content> pageUtils = contentClient.getContent_Page( pageIndex,pageSize);
        model.addAttribute("pageUtils",pageUtils);
        return "content";

    }

    /*
    * 修改广告的有效无效状态
    * */
    @RequestMapping("content_updateStatus")
    @ResponseBody
    public MessageResults updateStatus(@RequestParam(value = "id") Integer id,
                                       @RequestParam(value = "status") Integer status){
        MessageResults results = null;
//        System.out.println("id="+id+"status="+status);
        int integer = contentClient.updateStatus(id, status);
//        System.out.println(integer);
        if(integer>0){
            results = new MessageResults(200,"更新成功");
        }else {
            results = new MessageResults(500,"更新失败");
        }
        return results;
    }

    @RequestMapping("content_add")
    @ResponseBody
    public MessageResults content_add( Content content){
        MessageResults results = null;
        System.out.println("新增的对象是="+content);
        Integer integer = contentClient.addContent(content);
        if(integer>0){
            results = new MessageResults(200,"更新成功");
        }else {
            results = new MessageResults(500,"更新失败");
        }
        return results;
    }

    @RequestMapping("content_goUpdate")
    @ResponseBody
    public Map<String,Object> content_goUpdate(Integer id){
        //广告回显的现象
        Content content = contentClient.getContentById(id);
        //获取下拉框数据
        List<ContentCategory> contentCategorys = contentCategoryClient.getContentCategorys();
        HashMap<String,Object> map = new HashMap<>();
        map.put("content",content);
        map.put("contentCategorys",contentCategorys);
        return map;
    }

    @RequestMapping("content_update")
    @ResponseBody
    public MessageResults content_update(Content content){
        MessageResults results = null;
        System.out.println("更新的对象是="+content);
        Integer integer = contentClient.content_update(content);
        if (integer > 0) {
            results = new MessageResults(200, "更新成功");
        } else {
            results = new MessageResults(500, "更新失败");
        }
        return results;
    }
}
